<?php

/**
 * 
 * 
 * CREATE  TABLE IF NOT EXISTS `bayes_category` (
  `bayes_category_id` INT NOT NULL AUTO_INCREMENT ,
  `category_text` VARCHAR(80) NULL ,
  `category_feature_count` INT NULL ,
  `category_message_count` INT NULL ,
  PRIMARY KEY (`bayes_category_id`) ,
  UNIQUE INDEX `feature_text_UNIQUE` (`category_text` ASC) )
ENGINE = InnoDB;
 * 
 */
abstract class BayesDataTemplate extends ORM
{
	
	// Table primary key and value
	protected $primary_key				= '';

	protected $primary_val				= '';

	
	protected $table_names_plural       = FALSE;

	protected $table_name				= '';
	
	
	protected $exists					= FALSE; 
	
	protected $key_array				= FALSE;
	
	
	
	public function __construct( )
	{
		if(!empty($this->table_name))
		{
			parent::__construct();
		}
	}
	
	
	public function setup($table_name, $db_name, $key_array = array())
	{
		$this->db_name = $db_name;
		$this->table_name = $table_name;
		
		parent::__construct();
		
		if(!empty($key_array))
		{
			
			$this->set_text_key($key_array);
		}
	}
	
	
	public function set_text_key( $key_array )
	{
		$this->key_array = $key_array;
		$obj = $this->where($key_array)->find();
		$exists = TRUE;
		foreach($key_array as $key => $value)
		{
			if($exists)
			{
				$this->exists = TRUE;
			}
			if(!$this->$key)
			{
				$exists = FALSE;
				$this->exists = FALSE;
			}
			$this->$key = $value;
		}
		
		return $obj;
		
	}
	
	
	
	public function get()
	{
		return $this->key_array;
	}
	
	
	
	public function get_db_name()
	{
		return $this->db_name;
	}
	
	
	public function get_table_name()
	{
		return $this->table_name;
	}
	
	
	public function exists()
	{
		return $this->exists;
	}
	
	
	public function create()
	{
		$this->save();
		return $this->set_text_key($this->get());
	}
	
	
	public static function factory($name, $setup_arguments = array())
	{
		$object = new $name();
		
		if(!empty($setup_arguments))
		{
			call_user_func_array(array($object,'setup'), $setup_arguments);
		}
		
		return $object;
	}
	
	
	
	
	
}
